(function(){
    var domUtil = UE.dom.domUtils;
    var utils = UE.utils;
    var UIBase = baidu.editor.ui.UIBase;
    var editorui = baidu.editor.ui;
    var dialog = editorui.Dialog;
    var buttons = editorui.buttons;
    var domUtils = UE.dom.domUtils;

    function makeGetter(editor){
        return function(name){
            return $('#' + editor.container.id + '_' + name);
        };
    }

    function clearNodeStyle(root){
        if(root.type == 'element'){
            if(root.children && root.children.length){
                utils.each(root.children,function(item){
                    clearNodeStyle(item);
                });
            }
            root.setAttr('style', '');
        }
    }

    function toTextNode(node, text){
        node.data = text || '';
        node.children = undefined;
        node.type = 'text';
        node.tagName = undefined;
        node.attrs = {};
        return node;
    }

    function toEmptyNode(node) {
        toTextNode(node, '');
    }

    // 去掉微信文章链接的内容
    function clearWeiXinLink(root,tagName) {
        if(root.type === 'element'){
            if(root.children && root.children.length){
                utils.each(root.children,function (item) {
                    clearWeiXinLink(item,tagName);
                });
            }
            if(root.tagName === 'a'){
                var href = root.getAttr('href');
                console.log('a:::', href);
                if (href && href.indexOf('http://mp.weixin.qq.com') === 0) {
                    console.log('set text', root.innerText());
                    toTextNode(root, root.innerText());
                }
            }
        }
    }

    var InputFilterMap = {
        'remove-weixin-link': function(root){
            utils.each(root.children,function(item){
                clearWeiXinLink(item);
            });
        },
        'remove-weixin-img-src': function(root){
            var imgs = root.getNodesByTagName('img');
            utils.each(imgs,function(item){
                console.log(item);
                delete item.attrs['data-ratio'];
                delete item.attrs['data-s'];
                delete item.attrs['data-src'];
                delete item.attrs['data-type'];
                delete item.attrs['data-w'];
                delete item.attrs['data-type'];
                delete item.attrs['_width'];
                delete item.attrs['data-fail'];
                // 修改图片宽高属性
                var style = item.attrs['style'];
                if (style) {
                    var w = style.match(/width:(\d*)/);
                    if (w && w[1] > 640) {
                        style = style.replace(/width:.*?;/, '').replace(/height:.*?;/, '');
                        style += 'width:auto !important;max-width:100% !important;height:auto !important;';
                        item.attrs['style'] = style;
                    }
                }
            });
        }
    }

    UE.plugin.register('content-filter', function (){
        return {
            bindEvents:{
                'ready':function(){

                }
            },

           outputRule: function(root){

           },

           inputRule:function(root){
               if(this.options.inputFilter){
                   this.options.inputFilter.forEach(function(item){
                       console.log(item);
                       if(InputFilterMap[item]){
                           InputFilterMap[item](root);
                       }
                   });
               }
           },

           commands:{

           }
        }
    });

    // 扩展缩进
    function makeIndentCombox(uiName){
        //创建下拉菜单中的键值对，这里我用字体大小作为例子
        var items = [];
        for(var i= 0,ci;ci=[ 0.1,0.5, 1, 2, 3, 4][i++];){
            if (ci=='0.1') {ci='0'};
            items.push({
                //显示的条目
                label:' ' + ci,
                //选中条目后的返回值
                value:ci,
                //针对每个条目进行特殊的渲染
                renderLabelHtml:function () {
                    //这个是希望每个条目的字体是不同的

                    return '<div class="edui-label %%-label" style="text-align:center;line-height:2;font-size:12px;">' + (this.label || '') + '</div>';
                }
            });
        }
        return function(editor) {
            var combox = new UE.ui.Combox({
                //需要指定当前的编辑器实例
                editor:editor,
                className: 'tool-' + uiName,
                theme: editor.options.theme,
                //添加条目
                items:items,
                //当选中时要做的事情
                onselect:function (t, index) {
                    //拿到选中条目的值
                    editor.execCommand(uiName, this.items[index].value);
                },
                //提示
                title: '两端缩进',
                //当编辑器没有焦点时，combox默认显示的内容
                initValue: '两端缩进'
            });
            editorui.buttons[uiName] = combox;
            return combox;
        };
    }

    editorui['ex-indent'] = makeIndentCombox('ex-indent');

    UE.registerUI('ex-indent',function (editor,uiName) {
        var me = this;
        me.commands['margin'] = {
            execCommand : function(cmd,value) {
                 var me = this;
                 var value = me.queryCommandState("margin") ? "0em" : (me.options.indentValue || value);
                 me.execCommand('Paragraph','p',{style:'margin:'+ value});
            },
            queryCommandState : function() {
                var pN = domUtils.filterNodeList(this.selection.getStartElementPath(),'p h1 h2 h3 h4 h5 h6');
                return pN && pN.style.margin && parseInt(pN.style.margin) ?  1 : 0;
            }
        };

        editor.registerCommand(uiName, {
            execCommand:function(cmdName,value){
                //这里借用fontsize的命令
                this.execCommand('margin','0em '+value + "em");
                this.execCommand('justify', 'justify');
            },
            queryCommandValue:function(){
                //这里借用fontsize的查询命令
                return this.queryCommandValue('margin');
            }
        });
    });

})();
